package org.example.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import javax.sql.DataSource;

@Configuration
public class mybatisConfig {

    @Bean
//    注入salsessionfactry对象
    public static SqlSessionFactoryBean getSqlSessionFactory(DataSource dataSource) {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//        设置pojo包扫描别名路径
        bean.setTypeAliasesPackage("org.example.pojo");
//        注入数据库连接池
      //  bean.setConfigLocation(new ClassPathResource("mybatis.config.xml"));
        bean.setDataSource(dataSource);
        return bean;
    }

    @Bean
//    accountmapper会被注入到容器里
    public static MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
        scannerConfigurer.setBasePackage("org.example.mapper");
        return scannerConfigurer;
    }
}
